package com.tianji.remark.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.tianji.api.dto.remark.QueueLikerDTO;
import com.tianji.common.autoconfigure.mq.RabbitMqHelper;
import com.tianji.common.utils.StringUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.remark.constants.RedisConstants;
import com.tianji.remark.domain.dto.LikeRecordFormDTO;
import com.tianji.remark.domain.dto.LikedTimesDTO;
import com.tianji.remark.domain.po.LikedRecord;
import com.tianji.remark.mapper.LikedRecordMapper;
import com.tianji.remark.service.ILikedRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.models.links.Link;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.tianji.common.constants.MqConstants.Exchange.LIKE_RECORD_EXCHANGE;
import static com.tianji.common.constants.MqConstants.Key.LIKED_TIMES_KEY_TEMPLATE;


/**
 * <p>
 * 点赞记录表 服务实现类
 * </p>
 *
 * @author tc
 * @since 2023-04-16
 */
@Service
@RequiredArgsConstructor
public class LikedRecordServiceImpl extends ServiceImpl<LikedRecordMapper, LikedRecord> implements ILikedRecordService {


    private final RedisTemplate redisTemplate;
    private final RabbitMqHelper mqHelper;
    private final AmqpTemplate amqpTemplate;

    /**
     * 点赞或者取消点赞
     * @param formDTO
     */
    @Override
    public void likedRemark(LikeRecordFormDTO formDTO) {
        //判断是否是点赞记录
        Boolean su = formDTO.getLiked() ? liked(formDTO) : noliked(formDTO);
        //判断点赞是否存在
        if (!su) {
            return ;
        }
        //执行成功，统计总数
        Integer count =  lambdaQuery()
                .eq(LikedRecord::getBizId,formDTO.getBizId())
                .count();
        //MQ通知
        amqpTemplate.convertAndSend(LIKE_RECORD_EXCHANGE
                ,StringUtils.format(LIKED_TIMES_KEY_TEMPLATE,formDTO.getBizType())
                , QueueLikerDTO.of(formDTO.getBizId(),count));
    }
    /**
     * 查询点赞状态
     * @param bizIds
     * @return
     */
    @Override
    public Set<Long> userLiked(List<Long> bizIds) {
        //获取登录用户
        Long userId = UserContext.getUser();
        //查询点赞状态
        List<LikedRecord> list =  lambdaQuery()
                .eq(LikedRecord::getBizId,bizIds)
                .eq(LikedRecord::getUserId,userId)
                .list();
        //返回结果
        return list.stream().map(LikedRecord::getBizId).collect(Collectors.toSet());
    }



    //取消点赞
        private Boolean noliked (LikeRecordFormDTO formDTO) {
            //将点赞进行取消删除
            return remove(new QueryWrapper<LikedRecord>().lambda()
                    .eq(LikedRecord::getUserId,UserContext.getUser())
                    .eq(LikedRecord::getBizId,formDTO.getBizId())
                    .clone());
        }
        //点赞
        private Boolean liked (LikeRecordFormDTO formDTO) {
            //获取用户
            Long userId = UserContext.getUser();
            //查询点赞数据
            Integer count = lambdaQuery()
                    .eq(LikedRecord::getUserId, UserContext.getUser())
                    .eq(LikedRecord::getBizId, formDTO.getBizId())
                    .count();

            //判断是否有点赞
            if (count > 0) {
                return false;
            }
            //如果没有就进行增加点赞数据
            LikeRecordFormDTO l = new LikeRecordFormDTO();
            l.setBizId(userId);
            l.setLiked(l.getLiked());
            l.setBizType(l.getBizType());
            return true;
        }


}
